package com.example.template.utils;


import com.example.template.common.api.ApiCodeConstant;
import com.example.template.common.exception.GlobalException;
import com.example.template.common.snowflake.service.SnowflakeService;
import org.apache.commons.text.RandomStringGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @author xiadewang
 */
@Component
public class FilePathUtils {
	@Autowired
	SnowflakeService snowflakeService;
	private static SnowflakeService staticSnowflakeService;

	@PostConstruct
	public void init() {
		staticSnowflakeService = snowflakeService;
	}
	private static RandomStringGenerator randomString = new RandomStringGenerator.Builder().withinRange('a', 'z').build();



	/**
	 * 生成路径和文件名，路径为bathpath+日期（精确到天）
	 * 以当前时间开头加4位随机数的文件名
	 *
	 * @param originalFilename 原始文件名
	 * @return 10位长度文件名+文件后缀
	 */
//	public static String wholePathName(String basePath, String originalFilename) {
//		StringBuilder builder = new StringBuilder();
//		builder.append("/_signature/");
//        builder.append(DateUtil.getCurentDate());
//        builder.append("/");
//        builder.append(randomString.generate(4));
//		builder.append(System.currentTimeMillis());
//		builder.append(FileKit.getSuffix(originalFilename));
//		return builder.toString();
//	}

	/**
	 * 生成路径和文件名，路径为bathpath+雪花算法生成的ID+文件名后缀
	 *
	 * @param originalFilename 原始文件名
	 * @return 10位长度文件名+文件后缀
	 */
	public static String wholePathName(String basePath, String originalFilename) {
		StringBuilder builder = new StringBuilder();
		builder.append("/_signature/");
		builder.append(DateUtil.getCurentDate());
		builder.append("/");
		if(staticSnowflakeService.getId().getId()<0){
			throw new GlobalException(ApiCodeConstant.RESULT_ID_GEN_EXCEPTION,ApiCodeConstant.MSG_ID_GEN_EXCEPTION);
		}else{
			builder.append(staticSnowflakeService.getId().getId());
			builder.append(FileKit.getSuffix(originalFilename));
			return builder.toString();
		}

	}


	

	
}
